﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Chi.Lib.DBHelper;
using System.Collections.Generic;

public partial class ShowTableSchemaSMO : System.Web.UI.Page
{
    List<DBHelperTableSchema> SchemaLst = new List<DBHelperTableSchema>();
    Database db;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            db = DBHelperSMO.GetDBInstance();

            if (Request["tbName"] != null)
            { GetTableSchema(); }

            if (Request["ViewName"] != null)
            { GetViewSchema(); }

            if (SchemaLst.Count>0)
            {
                this.gvView.DataSource = SchemaLst;
                this.gvView.DataBind();
                this.pnl_TableSchema.Visible = true;
            }
        }// !IsPostBack
    }


    public void GetTableSchema()
    {
        Microsoft.SqlServer.Management.Smo.Table table = db.Tables[Request["tbName"]];

        this.ux_TBName.Text = table.Name;
        this.ux_CreateDate.Text = table.CreateDate.ToString();
        this.ux_LastModifyDate.Text = table.DateLastModified.ToString();

        foreach (Column col in table.Columns)
        {
            DBHelperTableSchema colSchema = new DBHelperTableSchema();
            colSchema.PK = col.InPrimaryKey ? "*" : "";
            colSchema.ColumnName = col.Name;
            colSchema.DataType = col.DataType.Name;
            colSchema.MaximunLength = col.DataType.MaximumLength.ToString();
            colSchema.NullAble = col.Nullable ? "Null" : "";
            colSchema.Default = col.Default;
            colSchema.Descriptions = col.ExtendedProperties.Contains("MS_Description") ?
                                    col.ExtendedProperties["MS_Description"].Value.ToString() : "";

            SchemaLst.Add(colSchema);
        }
    }

    public void GetViewSchema()
    {
        Microsoft.SqlServer.Management.Smo.View view = db.Views[Request["ViewName"]];

        this.ux_TBName.Text = view.Name;
        this.ux_CreateDate.Text = view.CreateDate.ToString();
        this.ux_LastModifyDate.Text = view.DateLastModified.ToString();

        foreach (Column col in view.Columns)
        {
            DBHelperTableSchema colSchema = new DBHelperTableSchema();
            colSchema.PK = col.InPrimaryKey ? "*" : "";
            colSchema.ColumnName = col.Name;
            colSchema.DataType = col.DataType.Name;
            colSchema.MaximunLength = col.DataType.MaximumLength.ToString();
            colSchema.NullAble = col.Nullable ? "Null" : "";
            colSchema.Default = col.Default;
            colSchema.Descriptions = col.ExtendedProperties.Contains("MS_Description") ?
                                    col.ExtendedProperties["MS_Description"].Value.ToString() : "";
            
            SchemaLst.Add(colSchema);
        }
    }

}
